﻿Imports System.Data.SqlClient
Public Class Order
    Dim cmd As New SqlCommand
    Private Sub Order_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        condb()
        txtcode.Focus()
        Dim d As Date = Now
        lbldaes.Text = d
        cmd.Connection = cn
        cmd.CommandText = "select top(1)InvoiceID  from tblorder order by InvoiceID desc "
        Dim invoice = cmd.ExecuteScalar
        lblinvoice.Text = invoice
        lblcasheair.Text = Login.txtusername.Text
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        grdsall.Rows.Clear()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub grdsall_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles grdsall.EditingControlShowing
        If Me.grdsall.CurrentCell.ColumnIndex = 0 And Not e.Control Is Nothing Then
            Dim tb As TextBox = CType(e.Control, TextBox)
            AddHandler tb.KeyDown, AddressOf txtcode_KeyDown
        End If
    End Sub

    Private Sub grdsall_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdsall.SelectionChanged
        cmd.Connection = cn
        cmd.CommandText = "select rial from tblmoney "
        Dim rial = cmd.ExecuteScalar

        Dim p As Double
        p = 0
        For i As Integer = 0 To Me.grdsall.Rows.Count - 1
            p = p + (Me.grdsall.Rows(i).Cells(1).Value * Me.grdsall.Rows(i).Cells(2).Value)
            grdsall.Rows(i).Cells(3).Value = (Me.grdsall.Rows(i).Cells(1).Value * Me.grdsall.Rows(i).Cells(2).Value)
        Next
        txtdolar.Text = p
        txtrial.Text = rial * p

        'for Discount
        txtdoladiscount.Text = txtdolar.Text
        txtrialdiscount.Text = txtrial.Text
    End Sub

    Private Sub Button6_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        cmd.Connection = cn
        cmd.CommandText = "insert into tblorder values('" & lbldaes.Text & "','" & lblcasheair.Text & "','" & txtdiscount.Text & "','" & txtdolar.Text & "','" & txtdoladiscount.Text & "')"
        cmd.ExecuteNonQuery()

        'for insert product
        Dim proname As String
        Dim price As Double
        Dim qty As Integer
        Dim total As Double

        cmd.Connection = cn
        cmd.CommandText = "select top(1)InvoiceID  from tblorder order by InvoiceID desc "
        Dim invoice = cmd.ExecuteScalar
        'For Insert Orderdetial
        For i = 0 To grdsall.Rows.Count - 1

            proname = grdsall.Rows(i).Cells(0).Value
            qty = Me.grdsall.Rows(i).Cells(1).Value
            price = grdsall.Rows(i).Cells(2).Value
            total = grdsall.Rows(i).Cells(3).Value

            cmd.Connection = cn
            cmd.CommandText = "insert into tblorderDetial values('" & invoice & "',N'" & proname & "','" & qty & "','" & price & "','" & total & "')"
            cmd.ExecuteNonQuery()
            'for select procode.................
            cmd.Connection = cn
            cmd.CommandText = "select procode from tblproduct where proname='" & proname & "'"
            Dim code = cmd.ExecuteScalar()
            'for update QTY
            cmd.Connection = cn
            cmd.CommandText = "update tblproduct set Qty= Qty-'" & qty & "' where procode='" & code & "' "
            cmd.ExecuteNonQuery()
        Next
        MessageBox.Show("Success")
    End Sub

    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click

    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Try
            grdsall.Rows.Remove(grdsall.CurrentRow)
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Button8_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Order_Load(sender, e)
        grdsall.Rows.Clear()
        txtdolar.Clear()
        txtrial.Clear()
        txtdiscount.Clear()
        txtrialdiscount.Clear()

    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Dim frm As New Invoice
        frm.ShowDialog()
    End Sub

    Private Sub txtdiscount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtdiscount.TextChanged
        condb()
        cmd.Connection = cn
        cmd.CommandText = "select rial from tblmoney "
        Dim rial = cmd.ExecuteScalar
        Try
            Dim dolar As Double
            Dim discount As Double
            Dim total As Double
            discount = txtdiscount.Text
            dolar = txtdolar.Text
            total = (dolar) - (dolar * discount \ 100)
            txtdoladiscount.Text = total
            txtrialdiscount.Text = rial * total
        Catch ex As Exception
        End Try

    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
        Me.Close()
    End Sub

    Private Sub txtcode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtcode.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                Dim a As Integer
                a = 1
                '..........for select product................
                cmd.Connection = cn
                cmd.CommandText = "select proName from tblProduct where Procode ='" & txtcode.Text & "'"
                Dim proname = cmd.ExecuteScalar

                '.....................................

                ' For Select Price............................................
                cmd.Connection = cn
                cmd.CommandText = "select priceout from tblProduct where Procode ='" & txtcode.Text & "'"
                Dim price = cmd.ExecuteScalar

                '.............................................................
                Me.grdsall.Rows.Add((proname), (a), (price), (total))
                txtcode.Clear()
                'for total money.............................
                cmd.Connection = cn
                cmd.CommandText = "select rial from tblmoney "
                Dim rial = cmd.ExecuteScalar
                Dim p As Double
                p = 0
                For i As Integer = 0 To Me.grdsall.Rows.Count - 1
                    p = p + (Me.grdsall.Rows(i).Cells(1).Value * Me.grdsall.Rows(i).Cells(2).Value)
                    grdsall.Rows(i).Cells(3).Value = (Me.grdsall.Rows(i).Cells(1).Value * Me.grdsall.Rows(i).Cells(2).Value)
                Next
                txtdolar.Text = p
                txtrial.Text = rial * p
                'for Discount
                txtdoladiscount.Text = txtdolar.Text
                txtrialdiscount.Text = txtrial.Text
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub grdsall_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdsall.CellContentClick

    End Sub
End Class